package com.itheima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.exception.LeadNewsException;
import com.itheima.common.util.RequestContextUtil;
import com.itheima.common.vo.PageResultVo;
import com.itheima.common.vo.ResultVo;
import com.itheima.dfs.template.DfsTemplate;
import com.itheima.wemedia.pojo.WmMaterial;
import com.itheima.wemedia.dto.WmMaterialPageRequestDto;
import com.itheima.wemedia.mapper.WmMaterialMapper;
import com.itheima.wemedia.service.WmMaterialService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.wemedia.service.WmNewsMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @version 1.0
 * @description <p>自媒体图文素材信息 业务实现</p>
 * @package com.itheima.wemedia.service.impl
 */
@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private WmMaterialService wmMaterialService;
    @Autowired
    private WmNewsMaterialService newsMaterialService;

    @Autowired
    private DfsTemplate dfsTemplate;

    @Value("${fastdfs-server}")
    private String fastdfsServer;

    /**
     * 素材列表 分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public PageResultVo<WmMaterial> pageList(WmMaterialPageRequestDto dto) {
        //1. 分页参数设置
        IPage<WmMaterial> pageInfo = new Page(dto.getPage(), dto.getSize());
        //2. 构建查询条件
        LambdaQueryWrapper<WmMaterial> queryWrapper = Wrappers.<WmMaterial>lambdaQuery()
                .eq(WmMaterial::getUserId, RequestContextUtil.getUserId())
                .eq(dto.getIsCollection() != null && dto.getIsCollection() != 0, WmMaterial::getIsCollection, dto.getIsCollection());

        //3. 分页查询
        page(pageInfo, queryWrapper);
        List<WmMaterial> wmMaterials = pageInfo.getRecords();
        //判断是否有素材
        if (!CollectionUtils.isEmpty(wmMaterials)) {
            //拼接路劲
            wmMaterials.forEach(m -> m.setUrl(fastdfsServer + m.getUrl()));
        }
        return PageResultVo.pageResult(dto.getPage(), dto.getSize(), pageInfo.getTotal(), wmMaterials);
    }


    /**
     * 更新
     *
     * @return
     */
    @Override
    public ResultVo updateIsCollection(Integer updateId) {
        if (updateId == 0 || updateId == null) {
            throw new LeadNewsException("更新失败！");
        }
        //根据id获取
        WmMaterial material = getById(updateId);

        if (null == material) {
            throw new LeadNewsException("信息不存在");
        }
        //收藏或者取消收藏
        material.setIsCollection(material.getIsCollection() == 1 ? 0 : 1);
        //返回结果
        LambdaQueryWrapper<WmMaterial> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmMaterial::getId,updateId);
        boolean wmMaterial = update(material,wrapper);
        if (wmMaterial){
            return ResultVo.ok();
        }
        return ResultVo.error();
    }


    /**
     * 删除素材
     *
     * @param id
     * @return
     */
    @Override
    public ResultVo delPictureById(Integer id) {
        //根据id获取
        WmMaterial wmMaterial = wmMaterialService.getById(id);
        //删除成功则
        if (wmMaterial!=null){
            wmMaterialService.removeById(id);
            String url = wmMaterial.getUrl();
            dfsTemplate.delete(url.substring(url.lastIndexOf("/")+1));
            return ResultVo.ok();
        }
        return ResultVo.error();
    }
}

